package com.cisetech.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cisetech.service.LoginService;
import com.cisetech.service.ResourcesService;
import com.cisetech.service.UserService;
import com.cisetech.service.CusTomerService;
import com.cisetech.vo.bobocai.LCustomer;
import com.cisetech.vo.bobocai.LResources;
import com.cisetech.vo.bobocai.LUser;
import com.cisetech.util.CreateMD5;
import com.cisetech.util.html.ResourcesHtmlUtil;
//登录逻辑层
@Service
public class LoginServiceImpl implements LoginService {
	@Autowired
	UserService userserviceImpl;
	@Autowired
	ResourcesService resourcesServiceImpl;
	@Autowired
	CusTomerService cusTomerService;
	
	
	@Override
	public String login(LUser user,HttpServletRequest request) {
		//判断用户是否存在
		if(user!=null && user.getName()!=null && !"".equals(user.getName())){
			List<LUser> list = userserviceImpl.findList(user);
			if(list==null || list.size()==0){
				return "1";//用户不存在
			}
			if(!(CreateMD5.getMd5(user.getPassword()).equals(list.get(0).getPassword()))){
				return "2";//密码不正确
			}
			//登录时将用户和用户权限（按钮）设置到session中
			userSetSession(list.get(0),request);
		}
		return "0";
	}
	//将用户放到session中
	public void userSetSession(LUser user,HttpServletRequest request) {
		request.getSession().setAttribute("luser", user);
		request.getSession().setAttribute("resourceshtml", getresources(user,request));
	}
	//将按钮（用户权限）放到session中
	public String getresources(LUser user,HttpServletRequest request){
		user.setAround("0");
		List<LResources> list = resourcesServiceImpl.findResourcesList(user);
		StringBuffer html = ResourcesHtmlUtil.getResourcesHtml(request,list);
		System.out.println(html.toString());
		return html.toString();
	}
	@Override
	public String qtLogin(LCustomer customer, HttpServletRequest request) {
		//判断用户是否存在
		if(customer!=null && customer.getEmail()!=null && !"".equals(customer.getEmail())){
			List<LCustomer> list = cusTomerService.findList(customer);
			if(list==null || list.size()==0){
				return "1";//用户不存在
			}
			if(!(customer.getPassword().equals(list.get(0).getPassword()))){
				return "2";//密码不正确
			}
			if(!"0".equals(list.get(0).getType())){
				return "3";//账号无效
			}
			//登录时将用户和用户权限（按钮）设置到session中
			cusTomerSetSession(list.get(0),request);
		}
		return "0";
	}
	//将用户放到session中
	public void cusTomerSetSession(LCustomer user,HttpServletRequest request) {
		request.getSession().setAttribute("LCustomer", user);
	}
	@Override
	public Map<String,String> register(LCustomer customer) {
		String stats = "1";
		Map<String,String> map = new HashMap<String,String>();
		if(customer==null){
			map.put("stats", stats);
			map.put("msg", "请输入账号密码");
			return map;
		}
		if(customer.getName()==null || "".equals(customer.getName())){
			map.put("stats", stats);
			map.put("msg", "请输入用户名");
			return map;
		}
		if(customer.getMobile()==null || "".equals(customer.getMobile())){
			map.put("stats", stats);
			map.put("msg", "请输入手机号");
			return map;
		}
		if(customer.getEmail()==null || "".equals(customer.getEmail())){
			map.put("stats", stats);
			map.put("msg", "请输入邮箱");
			return map;
		}
		if(customer.getPassword()==null || "".equals(customer.getPassword())){
			map.put("stats", stats);
			map.put("msg", "请输入密码");
			return map;
		}
		if(!customer.getPassword().equals(customer.getPassword1())){
			map.put("stats", stats);
			map.put("msg", "两次输入的密码不一致");
			return map;
		}
		map = registerDB(customer);
		return map;
	}
	@Override
	public Map<String, String> registerDB(LCustomer customer) {
		Map<String,String> map = new HashMap<String,String>();
		String stats = "1";
		map.put("stats", stats);
		List<LCustomer> list = cusTomerService.findList(customer);
		if(list!=null && list.size()>0){
			if( "0".equals(list.get(0).getType())){
				stats = "1";
				map.put("msg", "账号已经注册，请点击登录，或更换邮箱重新注册");
				map.put("stats", stats);
				return map;
			}else{
				customer.setId(list.get(0).getId());
				cusTomerService.updateByPrimaryKeySelective(customer);
			}
		}else{
			int i = cusTomerService.insert(customer);
		}
		stats = "0";
		map.put("stats", stats);
		map.put("msg", "恭喜您注册成功，请及时登录邮箱进行验证");
		return map;
	}
}
